package com.dlb.andorid.fragment;

import android.graphics.Color;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.dlb.andorid.App;
import com.dlb.andorid.ProductAdapter;
import com.dlb.andorid.R;
import com.dlb.andorid.XContract;
import com.dlb.andorid.activity.ProductDetailActivity;
import com.dlb.andorid.activity.SearchActivity;
import com.dlb.andorid.bean.ImgBean;
import com.dlb.andorid.bean.ProductBean;
import com.dlb.andorid.bean.SecTypeBean;
import com.dlb.andorid.bean.ServerResult;
import com.dlb.andorid.presenter.HomePresenter;
import com.kwan.xframe.mvp.view.fragment.BasePageItemFragment;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;


public class TypeFragment extends BasePageItemFragment implements XContract.IHomeView, OnLoadMoreListener, OnRefreshListener {

	public static TypeFragment newInstance() {
		TypeFragment fragment = new TypeFragment();
		Bundle args = new Bundle();
		fragment.setArguments(args);
		return fragment;
	}

	@Override
	protected String getItemTag() {
		return "Type";
	}

	RecyclerView rv_type, rv_home;
	View ll_search;
	HomePresenter mPresenter;
	List<SecTypeBean> mTypes = new ArrayList<>();
	List<ProductBean> mProducts = new ArrayList<>();
	BaseQuickAdapter<SecTypeBean, BaseViewHolder> mTypeAdapter;
	BaseQuickAdapter<ProductBean, BaseViewHolder> mHomeAdapter;
	int mCurrentSelectTypePosition = 0;

	protected RefreshLayout refreshLayout;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mPresenter = new HomePresenter(this);

		refreshLayout = mContentView.findViewById(R.id.refreshLayout);
		refreshLayout.setOnLoadMoreListener(this);
		refreshLayout.setOnRefreshListener(this);

		ll_search = mContentView.findViewById(R.id.ll_search);
		ll_search.setOnClickListener(this);
		rv_type = mContentView.findViewById(R.id.rv_type);

		mTypeAdapter = new BaseQuickAdapter<SecTypeBean, BaseViewHolder>(R.layout.list_item_home_type, mTypes) {
			@Override
			protected void convert(BaseViewHolder helper, SecTypeBean item) {

				if (mCurrentSelectTypePosition == helper.getLayoutPosition()) {
					helper.setBackgroundRes(R.id.tv_name, R.drawable.shape_home_type_selected);
					helper.setTextColor(R.id.tv_name, Color.WHITE);
				} else {
					helper.setBackgroundRes(R.id.tv_name, R.drawable.shape_home_type_normal);
					helper.setTextColor(R.id.tv_name, getResources().getColor(R.color.colorPrimary));
				}

				helper.setText(R.id.tv_name, item.getName());
			}
		};

		mTypeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
			@Override
			public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
				mCurrentSelectTypePosition = position;
				page = 1;
				isLoadMore = false;
				mPresenter.getProduct(mTypes.get(position).getId(), page, pageNum);
				mTypeAdapter.notifyDataSetChanged();
			}
		});
		rv_type.setLayoutManager(new LinearLayoutManager(mBaseActivity, LinearLayoutManager.HORIZONTAL, false));
		rv_type.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
		rv_type.setLayerType(View.LAYER_TYPE_HARDWARE, null);
		rv_type.setHasFixedSize(true);

		//解决更新 闪烁
		rv_type.setItemAnimator(null);
		rv_type.setNestedScrollingEnabled(false);
		rv_type.setAdapter(mTypeAdapter);

		rv_home = mContentView.findViewById(R.id.rv_home);
		mHomeAdapter = new ProductAdapter(mProducts);

		rv_home.setLayoutManager(new GridLayoutManager(mBaseActivity, 2, LinearLayoutManager.VERTICAL, false));
		rv_home.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
		rv_home.setLayerType(View.LAYER_TYPE_HARDWARE, null);
		rv_home.setLayerType(View.LAYER_TYPE_NONE, null);
		rv_home.setHasFixedSize(true);

		//解决更新 闪烁
		rv_home.setItemAnimator(null);
		rv_home.setNestedScrollingEnabled(false);
		rv_home.setAdapter(mHomeAdapter);

		mHomeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
			@Override
			public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
				Bundle bundle = new Bundle();
				bundle.putLong("id", mProducts.get(position).getId());
				go2Activity(ProductDetailActivity.class, bundle, false);
			}
		});

		mHomeAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
			@Override
			public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
				//toastMsg(""+mProducts.get(position).getId());
				Log.e("kwan", "car:" + mProducts.get(position).getId());
				if (view.getId() == R.id.iv_car) {
					Log.e("kwan", "car:" + mProducts.get(position).getId());
					mBaseActivity.showProgress("");
					mPresenter.addCar(mProducts.get(position).getId(), App.mUserInfo.getUid(), 1, mProducts.get(position).getType_id(), mProducts.get(position).getSale_price());
				}

			}
		});


	}

	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public void setUserVisibleHint(boolean isVisibleToUser) {
		super.setUserVisibleHint(isVisibleToUser);
		if (isVisibleToUser) {
			if (mBaseActivity != null && mPresenter != null) {
				//mBaseActivity.showProgress("");
				mPresenter.getSecType(classId);
			}
		}
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		if (v == ll_search) {
			go2Activity(SearchActivity.class, null, false);
		}
	}


	@Override
	public void onPause() {
		super.onPause();
	}

	@Override
	public void onStop() {
		super.onStop();
	}


	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	@Override
	protected int getContentViewId() {
		return R.layout.fragment_type;
	}


	public static int classId = 1;

	//第一次获取数据
	@Override
	public void fetchData() {
		mBaseActivity.showProgress("");
		mPresenter.getSecType(classId);
	}


	@Override
	public void onDestroyView() {
		super.onDestroyView();
	}

	@Override
	public String getPageName() {
		return null;
	}

	@Override
	public void getBannerSuccess(List<ImgBean> result) {

	}


	@Override
	public void getSecTypeSuccess(List<SecTypeBean> data) {
		dismissProgress();
		mTypes.clear();

		if (data == null) {
			toastMsg("没有记录");
		} else {
			mTypes.addAll(data);
			mPresenter.getProduct(mTypes.get(0).getId(), 1, pageNum);
		}

		mTypeAdapter.notifyDataSetChanged();

	}


	@Override
	public void getProductsSuccess(List<ProductBean> data) {
		dismissProgress();
		refreshLayout.finishRefresh();
		refreshLayout.finishLoadMore();

		if (isLoadMore) {

			if (data == null)
				return;

			mProducts.addAll(data);
			mHomeAdapter.setNewData(mProducts);
		} else {

			mProducts.clear();
			if (data != null)
				mProducts.addAll(data);

			mHomeAdapter.setNewData(mProducts);
		}

	}

	int page = 1;
	int pageNum = 10;
	boolean isLoadMore;

	@Override
	public void onRefresh(@NonNull RefreshLayout refreshLayout) {
		page = 1;
		isLoadMore = false;
		mPresenter.getProduct(mTypes.get(mCurrentSelectTypePosition).getId(), page, pageNum);
	}

	@Override
	public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
		isLoadMore = true;
		page++;
		mPresenter.getProduct(mTypes.get(mCurrentSelectTypePosition).getId(), page, pageNum);
	}

	@Override
	public void addCarSuccess(ServerResult data) {
		dismissProgress();
		toastMsg(data.getMsg());
	}


}
